package com.ruoyi.employee.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.employee.domain.Employee;
import com.ruoyi.employee.service.IEmployeeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-11
 */
@RestController
@RequestMapping("/ruoyi/employee")
public class EmployeeController extends BaseController
{
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 获取当前登录员工的个人信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:employee:emp_profile')") // 权限控制
    @GetMapping("/emp_profile")
    public AjaxResult employeeProfile() {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();

        // 通过用户ID查询关联的员工信息（需实现关联逻辑）
        Employee employee = employeeService.selectEmployeeByUserId(loginUser.getUserId());

        if (employee == null) {
            return AjaxResult.error("员工信息不存在");
        }
        return AjaxResult.success(employee);
    }


    /**
     * 查询员工信息列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(Employee employee)
    {
        startPage();
        List<Employee> list = employeeService.selectEmployeeList(employee);
        return getDataTable(list);
    }

    /**
     * 导出员工信息列表
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:employee:export')")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Employee employee)
    {
        List<Employee> list = employeeService.selectEmployeeList(employee);
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        util.exportExcel(response, list, "员工信息数据");
    }

    /**
     * 获取员工信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:employee:query')")
    @GetMapping(value = "/{employeeId}")
    public AjaxResult getInfo(@PathVariable("employeeId") Long employeeId)
    {
        return success(employeeService.selectEmployeeByEmployeeId(employeeId));
    }

    /**
     * 新增员工信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:employee:add')")
    @Log(title = "员工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Employee employee)
    {
        return toAjax(employeeService.insertEmployee(employee));
    }

    /**
     * 修改员工信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:employee:edit')")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Employee employee)
    {
        return toAjax(employeeService.updateEmployee(employee));
    }

//    /**
//     * 删除员工信息
//     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:employee:remove')")
//    @Log(title = "员工信息", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{employeeIds}")
//    public AjaxResult remove(@PathVariable Long[] employeeIds)
//    {
//        return toAjax(employeeService.deleteEmployeeByEmployeeIds(employeeIds));
//    }

    /**
     * 删除员工方法
     */
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{employeeIds}")
    public AjaxResult remove(@PathVariable Long[] employeeIds) {
        // 调用服务层删除并返回结果
        return toAjax(employeeService.deleteEmployeeByEmployeeIds(employeeIds));
    }

    /**
     * 状态修改
     */
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Employee employee)
    {
        // 修改操作
        employee.setUpdateBy(getUsername());
        return toAjax(employeeService.updateEmployeeStatus(employee));
    }
}
